/**
 * 
 */
package com.qccx.ueuo.selfguide.action;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.qccx.ueuo.selfguide.entity.User;

/**
 * 功能介绍:
 * @author rainJohn
 *
 * @date [2013-4-28]
 */
public class UserAction extends BaseAction
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */
    private User user;
    
    public UserAction()
    {
        
    }
    
    
    public String login()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        try
        {
            user = this.getUserService().queryUserByName( user);
            request.getSession().setAttribute( "user", user );
        } catch ( SQLException e )
        {
            e.printStackTrace();
        }
        return "success";
    }
    
    /**
     * 检测用户名是否存在
     * @return
     */
    public String checkLogin()
    {
        User u;
        HttpServletRequest request = ServletActionContext.getRequest();
        try
        {
            u = this.getUserService().queryUserByName( user);
            Map<String,String> data = new HashMap<String, String>();
            if(u == null || !user.getPwd().equals( u.getPwd() ))
            {
                data.put( "msg", "failed" );
                data.put( "value", "不存在该用户名或用户名密码错误,请重新输入!" );
                return "failed";
            }
            else
            {
                data.put( "msg", "success" );
                data.put( "value", "正在登录中..." );
                user = u;
                request.setAttribute( "data", data );
                request.setAttribute( "user", user );
                return "success";
            }
        } catch ( SQLException e )
        {
            e.printStackTrace();
            return "input";
        }
    }
    
    
    
    public User getUser()
    {
        return user;
    }
    public void setUser( User user )
    {
        this.user = user;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
     */
    @Override
    public void setSession( Map<String, Object> arg0 )
    {
        
    }


    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public Object getModel()
    {
        return User.class;
    }


    /* (non-Javadoc)
     * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
     */
    @Override
    public void setServletRequest( HttpServletRequest arg0 )
    {
        // TODO Auto-generated method stub
        
    }


    /* (non-Javadoc)
     * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
     */
    @Override
    public void setServletResponse( HttpServletResponse arg0 )
    {
        // TODO Auto-generated method stub
        
    }
}
